import HTMLParser
import urllib
import re


urlString="http://www.uio.no/tjenester/it/nett/fastnett/nix/nix-ops.html"
check=False
check2=False
check3=False
urlText=[]

class ParseIXP(HTMLParser.HTMLParser):
    def handle_starttag(self,tag,attrs):
        global check
        global check2
        if tag=="h2":
            check2=True
        if tag=="td":
            check=True
                  
    def handle_endtag(self,tag):
        global check2
        global check3
        if tag=="h2" and check2:
            check3=True
        if tag=="h2" and not check2:
            check3=False
              
    def handle_data(self,data):
        global check
        global check2
        global check3
        if data!='\n' and check2:
            data=data.lstrip()
            if(data !="ISPs connected to TRDIX, BIX, TIX and SIX"):
                check2=False
        if data!='\n' and check==True and check3:
                if (re.match("\s*\d\d*$",data)):
                    data=data.lstrip()
                    urlText.append(data)
                check=False    
           
    def unknown_decl(self,data):
        pass
            
lparser=ParseIXP()
lparser.feed(urllib.urlopen(urlString).read())


filePath="ixp_members.txt"
file = open(filePath,"a")
for item in urlText:
    #print item
    print >> file, "168 %s" % item
file.close()